package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.BaseInfoLogic;
import cn.topevery.garden.entity.dto.BaseInfoDTO;
import cn.topevery.garden.entity.oneMap.CountVO;
import cn.topevery.garden.entity.po.BaseInfo;
import cn.topevery.garden.entity.query.BaseInfoQuery;
import cn.topevery.garden.entity.vo.BaseInfoVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author code-generate
 * @date 2022-01-05
 */
@Api(tags = "园林绿化城市基础数据")
@Slf4j
@RestController
@RequestMapping("/baseInfo")
public class BaseInfoController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    BaseInfoLogic baseInfoLogic;


    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取页列表")
    @PostMapping("/getPage")
    public JsonResult<DataPager<BaseInfoVO>> listPage(@RequestBody BaseInfoQuery query) {
        return JsonResult.ok(baseInfoLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody BaseInfoDTO dto) {
        baseInfoLogic.saveOrUpdate(getUser().id.toString(), mapper.map(dto, BaseInfo.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<BaseInfoVO> getInfoById(@RequestParam Integer id) {
        BaseInfo entity = baseInfoLogic.getEntity(id);
        BaseInfoVO vo = mapper.map(entity, BaseInfoVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        baseInfoLogic.deleteBatchById(getUser().id.toString(), ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出信息")
    @GetMapping("/export")
    public void export(BaseInfoQuery query, HttpServletResponse response) {
        List<BaseInfoVO> list = baseInfoLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "", "sheet1", BaseInfoVO.class, "", response);
    }

    @ApiOperation("概况")
    @GetMapping("/survey")
    public JsonResult survey() {
        BaseInfo entity = baseInfoLogic.getEntity(1);
        // 判断数据更新时间，超过5分钟，自动更新一次
        Duration between = Duration.between(entity.getUpdateTime(), LocalDateTime.now());
        if (between.toMinutes() > 5) {
            // 护栏，休息亭，座椅
            List<String> types = Arrays.asList("GS1", "GS4", "GS3");
            List<CountVO> countVO = baseInfoLogic.getSurvey(types);
            HashMap<String, Integer> countMap = new HashMap<>();
            for (CountVO vo : countVO) {
                countMap.put(vo.getName(), vo.getValue());
            }
            entity.setGreenGuardrailTotal(countMap.getOrDefault("GS1", 0));
            entity.setPavilionTotal(countMap.getOrDefault("GS4", 0));
            entity.setChairTotal(countMap.getOrDefault("GS3", 0));
            baseInfoLogic.update("0", entity);
        }
        LinkedHashMap<String, Number> baseInfo = new LinkedHashMap<>(9);
        baseInfo.put("greenCoverage", entity.getGreenCoverage());
        baseInfo.put("greeningRate", entity.getGreeningRate());
        baseInfo.put("parkPerCapital", entity.getParkPerCapital());
        baseInfo.put("greenTotal", entity.getGreenTotal());
        baseInfo.put("parkTotal", entity.getParkTotal());
        baseInfo.put("squareTotal", entity.getSquareTotal());
        baseInfo.put("greenGuardrailTotal", entity.getGreenGuardrailTotal());
        baseInfo.put("pavilionTotal", entity.getPavilionTotal());
        baseInfo.put("chairTotal", entity.getChairTotal());

        return JsonResult.ok(baseInfo);
    }
}
